#include <iostream>
#define MAXN 100
using namespace std;
int w[MAXN], v[MAXN];
int N, W;
int dp[MAXN][MAXN];
void solve();
int main() {
    cin >> N;
    for (int i = 0; i < N; i++) {
        cin >> w[i] >> v[i];
    }
    cin >> W;
    solve();
}

// 从前i件挑, 质量小于j的最大值
void solve() {
    for (int i = 0; i < N; i++) {
        for (int j = 0; j <= W; j++) {
            dp[i + 1][j] = dp[i][j];
            for (int k = 0; k * w[i] <= j; k++) {
                dp[i + 1][j] = max(dp[i + 1][j], dp[i][j - k * w[i]] + k * v[i]);
            }
        }
    }
    cout << dp[N][W];
}